import { media } from "@kit.MediaKit";
import { songItemType } from "../Model/Song";
import {playStateType} from "../Model/playState"
import { SONG_KEY } from "../contants";
import { router } from "@kit.ArkUI";

export class AvPlayerManager{

  //创建播放器
  static player:media.AVPlayer|null=null
  //创建播放歌曲对象
  static currentSong:playStateType=new playStateType()
  //初始化
  static async init(){
    if(!AvPlayerManager.player){
      AvPlayerManager.player=await media.createAVPlayer()
    }
    //监听播放状态
    AvPlayerManager.player.on("stateChange",(e)=>{
      if(e == 'initialized'){
        AvPlayerManager.player?.prepare()
      }else if(e == 'prepared'){
        AvPlayerManager.player?.play()
      AvPlayerManager.currentSong.isPlay=true
      }else if(e == 'completed'){
        AvPlayerManager.nextPlay(AvPlayerManager.currentSong.playMode == 'repeat')
      }
    })
    AvPlayerManager.player.on("durationUpdate",(e)=>{
      AvPlayerManager.currentSong.duration=e
      console.log(e.toString())
    })
    AvPlayerManager.player.on("timeUpdate",(e)=>{
      AvPlayerManager.currentSong.time=e
      console.log("time+"+e)
      AppStorage.setOrCreate(SONG_KEY,AvPlayerManager.currentSong)
    })
  }
  //播放方法
  static singPlay(song:songItemType){
    // AvPlayerManager.player!.url=song.url
    const inList=AvPlayerManager.currentSong.playList.some(item=>item.id==song.id)
    if(inList){
      //是否正在播放的歌曲
      if(AvPlayerManager.currentSong.url == song.url){
        AvPlayerManager.player?.play()
        AvPlayerManager.currentSong.isPlay=true
      }else{
        const index=AvPlayerManager.currentSong.playList.findIndex(v=>v.id==song.id)
        //找到要播放的歌曲的索引
        AvPlayerManager.currentSong.playIndex=AvPlayerManager.currentSong.playList.findIndex(item => item.id == song.id)
        AvPlayerManager.changePlay()
      }
    }else{
      AvPlayerManager.currentSong.playList.unshift(song)
      AvPlayerManager.currentSong.playIndex=0
      AvPlayerManager.changePlay()

    }
    AppStorage.setOrCreate(SONG_KEY,AvPlayerManager.currentSong)
  }
  static async changePlay(){
    await AvPlayerManager.player?.reset()
    AvPlayerManager.currentSong.duration=0
    AvPlayerManager.currentSong.time=0
    AvPlayerManager.currentSong.img=AvPlayerManager.currentSong.playList[AvPlayerManager.currentSong.playIndex].img
    AvPlayerManager.currentSong.name=AvPlayerManager.currentSong.playList[AvPlayerManager.currentSong.playIndex].name
    AvPlayerManager.currentSong.url=AvPlayerManager.currentSong.playList[AvPlayerManager.currentSong.playIndex].url
    AvPlayerManager.currentSong.author=AvPlayerManager.currentSong.playList[AvPlayerManager.currentSong.playIndex].author
    AvPlayerManager.player!.url=AvPlayerManager.currentSong.url

  }
  //播放方法
  static stopPlay(){
    AvPlayerManager.player!.url=""
  }
  static async pause(){
    AvPlayerManager.player?.pause()
    AvPlayerManager.currentSong.isPlay=false
    AppStorage.setOrCreate(SONG_KEY,AvPlayerManager.currentSong)
  }
  static prevPlay(){
    if(AvPlayerManager.currentSong.playMode == 'random'){
      AvPlayerManager.currentSong.playIndex=Math.floor(Math.random()*AvPlayerManager.currentSong.playList.length)
    }else{
      AvPlayerManager.currentSong.playIndex--
      if(AvPlayerManager.currentSong.playIndex<0){
        AvPlayerManager.currentSong.playIndex=AvPlayerManager.currentSong.playList.length-1
      }
    }
    AvPlayerManager.singPlay(AvPlayerManager.currentSong.playList[AvPlayerManager.currentSong.playIndex])
  }
  static nextPlay (repeat?: boolean) {
    if (!repeat) {
      if (AvPlayerManager.currentSong.playMode == 'random') {
        AvPlayerManager.currentSong.playIndex = Math.floor(Math.random() * AvPlayerManager.currentSong.playList.length)
      } else {
        // 切到前一个
        AvPlayerManager.currentSong.playIndex++
        if (AvPlayerManager.currentSong.playIndex > AvPlayerManager.currentSong.playList.length - 1) {
          AvPlayerManager.currentSong.playIndex = 0
        }
      }
    }
    AvPlayerManager.singPlay(AvPlayerManager.currentSong.playList[AvPlayerManager.currentSong.playIndex])
  }
  // 列表删除歌曲
  static remove (index: number) {
    if (index === AvPlayerManager.currentSong.playIndex) {
      if (AvPlayerManager.currentSong.playList.length > 1) {
        AvPlayerManager.currentSong.playList.splice(index, 1)
        // (下标 + 长度) % 长度
        AvPlayerManager.currentSong.playIndex = (AvPlayerManager.currentSong.playIndex + AvPlayerManager.currentSong.playList.length) % AvPlayerManager.currentSong.playList.length
        AvPlayerManager.singPlay(AvPlayerManager.currentSong.playList[AvPlayerManager.currentSong.playIndex])
      } else {
        router.back()
        // 初始化播放信息
        AvPlayerManager.currentSong = new playStateType()
        AvPlayerManager.player?.reset()
      }
    } else {
      // 如果删除的歌曲的索引小于正在播放的歌曲
      if (AvPlayerManager.currentSong.playIndex > index) {
        AvPlayerManager.currentSong.playIndex--
      }
      AvPlayerManager.currentSong.playList.splice(index, 1)
    }
    AppStorage.setOrCreate(SONG_KEY, AvPlayerManager.currentSong)
  }
}